package domain.web.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import core.constant.SystemConstant;

import process.model.DetailP;
import process.model.lookup.ProductLookup;
import process.module.OntologyBuilder;

import domain.web.dao.CategoryDao;
import domain.web.dao.DetailDao;
import domain.web.dao.ManufacturerDao;
import domain.web.dao.ProductDao;
import domain.web.model.Category;
import domain.web.model.CategoryF;
import domain.web.model.Detail;
import domain.web.model.Product;
import domain.web.model.ProductView;
import domain.web.service.ProductService;

@Service("productService")
public class ProductServiceImpl implements ProductService {

	@Autowired
	private ProductDao productDao;
	@Autowired
	private CategoryDao categoryDao;
	@Autowired
	private ManufacturerDao manufacturerDao;
	@Autowired
	private DetailDao detailDao;

	@Override
	public ProductView getView(long id) {
		return productDao.getView(id, "vi");
	}

	@Override
	public List<CategoryF> getCategories() {
		return categoryDao.getDepthCategory("en");
	}

	@Override
	public List<Category> getBreadcrumbsCategories(long id) {
		return categoryDao.getBreadcrumbsCategories(id, "en");
	}

	@Override
	public void insert(ProductLookup lookup, long storeId) throws Exception {
		OntologyBuilder ontologyBuilder;
		ontologyBuilder = new OntologyBuilder(SystemConstant.O_FILE);

		// INSERT TO ONTOLOGY
		ontologyBuilder.addIndividual(lookup.getUri(), lookup.getClassName(),
				lookup.getManufacturer());

		// INSERT TO DB
		long catId = categoryDao.get(lookup.getClassName(), "en").getId();
		// long manuId = manufacturerDao.get(
		// OntoQuery.getClearName(lookup.getManufacturer())).getId();
		long manuId = manufacturerDao.getByUri(
				lookup.getManufacturer().replace(" ", "_")).getId();

		Product product = new Product();
		product.setName(lookup.getName());
		product.setUri(lookup.getUri());
		product.setCategoryId(catId);
		product.setManufacturerId(manuId);
		product.setImageUrl("");
		long id = productDao.insert(product);
		lookup.setId(id);
		System.out.println("--- NEW PRODUCT:" + product.getName() + "  PId:"
				+ id + " ---");
		
		update(lookup, storeId);

	}

	private void update(ProductLookup lookup, long storeId) {
		System.out.println("Select URI with: " + lookup.getUri());
		Product product = productDao.get(lookup.getUri());
		if (product == null) {
			return;
		}
		System.out.println("--- FOUND PRODUCT: Name=" + lookup.getName()
				+ " Id=" + product.getId() + " ---");
		lookup.setId(product.getId());

		Detail detail = detailDao.get(product.getId(), storeId);
		// Check if product and Store existed in table Details

		if (detail == null) {
			// If not existed => Insert new
			try {
				detail = new Detail(product.getId(), storeId,
						lookup.getPrice(), "", "");
				// insert into DB
				System.out
						.println("\t==> DETAIL Inserted with PId:"
								+ detail.getProductId() + " / StoreId:"
								+ detail.getStoreId() + " / Price:"
								+ detail.getPrice());
				detailDao.insert(detail);

				// update Product images
				if (product.getImageUrl().isEmpty()) {
					product.setImageUrl("");
					productDao.update(product);
				}
			} catch (Exception ex) {
				System.out.println(ex.getMessage());
			}
		} else {
			// If existed => Update
			detail.setPrice(lookup.getPrice());
			// Update to DB
			System.out.println("\t==> DETAIL Updated with PId:"
					+ detail.getProductId() + " / StoreId:"
					+ detail.getStoreId() + " / Price:" + detail.getPrice());
			detailDao.update(detail);

			// update Product images
			// product.setImageUrl(detailP.getImageUrl());
			// productDao.update(product);
		}
	}

}
